<?php
class Admin_ExpenditureTypesController extends Model3_Controller
{
    private $_credentials;
   // protected $_config;
    
    public function init()
    {
        if(!Model3_Auth::isAuth())
            $this->redirect();
        else
        {
            $role = Model3_Auth::getCredentials('user_type');
            if( $role !== DefaultDb_Entity_User::TYPE_ADMIN )
            {
               Model3_Auth::deleteCredentials();
               $this->redirect();
            }
        }
        $this->_credentials = Model3_Auth::getCredentials();
        $this->view->setTemplate('Catalog');
    }
    
    public function indexAction()
    {
    	
    }
	
	public function formExpenditureTypesAction()
    {
    	
    }
    
	/* Jorge Tapia*/
/* funcion para rellenar la tabla kendo*/ 
 public function getListExpenditureTypesAction(){
    	
    	$this->view->setUseTemplate(FALSE);
    	header("Content-Type: application/json");
    
       	$em = $this->getEntityManager('DefaultDb');
       	$lista = $em->getRepository('DefaultDb_Entity_ExpenditureType')->findAll();
          	
    	$arrJson = array();
    	foreach($lista as $item)
    	{
    		$temp = array(
    				"expenditureTypeID" => $item->getId(),
    				"name" => $item->getName(),
    				"description" => $item->getDescription(),
					"companie" => $item->getCompanie(),
    				"date" => $item->getDate()
    			

    		);
    		array_push($arrJson, $temp);
    	}
		
		
    	$this->view->result = json_encode($arrJson);
    }    
   
/* Jorge Tapia*/
/* funcion para guardar un nuevo tipo de gasto*/
 public function saveExpenditureTypesAction() {
        $this->view->setUseTemplate(FALSE);
        $post= $this->getRequest()->getPost();
      
		$request= explode("!", $post['form']);
    	$form= explode("*",$request[0]);
    	$action= $request[1];
    	$id_expenditure_types=$request[2];     	
        $em= $this->getEntityManager('DefaultDb');
        $user= $em->getRepository('DefaultDb_Entity_User')->findOneBy(array('id_users' => $this->_credentials));
        
		if ($action == "btnSaveExpenditure")
    	{
		$expenditure= new DefaultDb_Entity_ExpenditureType();
		$expenditure->setDate(date('Y-m-d H:m:s'));


		}
		if ($action == "btnEditExpenditure")
    	{
	  		$expenditure = ($id_expenditure_types) ? $em->getRepository('DefaultDb_Entity_ExpenditureType')->findOneBy(array('id_expenditure_types' => $id_expenditure_types)) : new DefaultDb_Entity_ExpenditureType();
    	}
		    	$em->getConnection()->beginTransaction();
try{
				$expenditure->setName($form[0]);
                $expenditure->setDescription($form[1]);
				

				$expenditure->setCompanie($user->getId());

              	$em->persist($expenditure);
        		$em->flush();
				$em->getConnection()->commit();
				$result="Datos Guardados.";
}
				
				catch(Exception $e)
    	{
    		$em->getConnection()->rollback();
    		$em->close();
    		$result = "Error al Guardar los Datos";
    	}
				
    			$this->view->result=$result;
    }


/* Jorge Tapia*/
/* funcion para editar  tipo de gasto*/
    	public  function  editExpenditureTypesAction()
    {
    	if( !is_null($this->getRequest()->getParam('idexpenditure')) && is_numeric($this->getRequest()->getParam('idexpenditure')) )
    	{
    		$id_expenditure_types = $this->getRequest()->getParam('idexpenditure');
    		$em = $this->getEntityManager('DefaultDb');
    		$expenditure = $em->getRepository('DefaultDb_Entity_ExpenditureType')->findOneBy(array('id_expenditure_types' => $id_expenditure_types));
    		
    		$this->view->idexpenditure = $id_expenditure_types;
    		$this->view->name = $expenditure->getName();
    		$this->view->description = $expenditure->getDescription();
    		$this->view->date = date("Y-m-d", strtotime($expenditure->getDate()));
    		
    	
    	}
	}
    	


 
}